package components.editor
{
	import UIFree.uif_component;
	import UIFree.uif_fonts;
	
	import UIUtils.uif_util;
	
	import classes.Commands.c_applyText;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class c_textElement extends c_baseElement {
		public static const DEFAULTTEXTFORMAT: Object = {
			font	: 'GOTHIC',
			size	: 24,
			color	: 0
		}
		
		private var _textField	: TextField;
		private var _prevText	: String;
		public function c_textElement(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function nameClass(): String {
			return locale.TEXT;
		}
		
		override protected function createComponents():void {
			super.createComponents();
			_textField = createTextLayer();
			addEvent(TextEvent.TEXT_INPUT, doChangeText, _textField);
//			addEvent(KeyboardEvent.KEY_DOWN, doFielTextKeyboard, _textField);
		}
		
		private function createTextLayer(): TextField {
			var a_textField: TextField = new TextField();
			a_textField.type 		= 'input';
			a_textField.embedFonts 	= true;
			a_textField.autoSize 	= TextFieldAutoSize.LEFT;
			a_textField.multiline	= true;
			a_textField.selectable 	= false;
			addChild(a_textField);
			return a_textField;
		}
/*		
		public function get textField(): TextField {
			return _textField;
		}
*/		
		
		public function getTextFormat(): TextFormat {
			return _textField.defaultTextFormat;
		}
		
		public function applyTextFormat(doAfterApply: Function, textFormat: Object=null): void {
			var df: TextFormat = _textField.defaultTextFormat;
			textFormat = uif_util.union({
				font	: df.font,
				size	: df.size,
				color	: df.color
			}, textFormat?textFormat:DEFAULTTEXTFORMAT);
			
			uif_fonts.applyFont(textFormat.font, _textField, textFormat.size, textFormat.color, function(): void {
				uif_util.applyTextFormat(_textField, textFormat);
				afterChangeText();
				if (doAfterApply != null) doAfterApply();
			});
		}
		
		private function doChangeText(e: Event): void {
			afterChangeText();
		}
		
/*		
		private function doFielTextKeyboard(e: KeyboardEvent): void {
			if (!e.ctrlKey) {
				if (e.keyCode == 13) {
					e.stopPropagation();
					releaseEditMode();
				}
			}
		}
*/		

		
		
		override public function get editMode(): Boolean {
			return _textField.selectable;
		}
		
		override protected function setEditMode(a_set: Boolean): void {
			if (a_set) enterEditMode();
		}
		
		protected function releaseEditMode(): void {
			_textField.selectable 	= false;
			editor.dragRect.link 	= this;
			executeCommand(new c_applyText(this, text, _prevText));
		}
		
		protected function enterEditMode(): void {
			_textField.selectable 	= true;
			editor.dragRect.link 	= null;
			_prevText 				= _textField.text;
			stage.focus 			= _textField;
			_textField.addEventListener(FocusEvent.FOCUS_OUT, doFocusOut);
		}
		
		protected function doFocusOut(e: Event): void {
			_textField.removeEventListener(FocusEvent.FOCUS_OUT, doFocusOut);
			releaseEditMode();
		}
		
		protected function afterChangeText(): void {
			resize(_textField.textWidth, _textField.textHeight);
		}
				
		public function get text(): String {
			return _textField.text;
		}
		
		public function set text(a_text: String): void {
			_textField.text = a_text;
			afterChangeText();
		}
	}
}